from datetime import datetime

from expiringdict import ExpiringDict

conversation_cache = ExpiringDict(max_len=1000000, max_age_seconds=180000)
session_id_msg = ExpiringDict(max_len=1000000, max_age_seconds=1800)


def put_conversation(message, role, session_id=None, request_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],
                     user_question='', sales_skill_name=''):
    conversation_history = []
    if session_id not in conversation_cache:
        conversation_cache[session_id] = []
    if message:
        message = message.replace('\n', '').replace(' ', '')
        conversation_dict = {"role": role, "content": message, "message_time": request_time,
                             "user_question": user_question, "sales_skill_name": sales_skill_name}
        conversation_cache[session_id].append(conversation_dict)
        conversation_history = conversation_cache.get(session_id)
    return conversation_history


def get_conversation(session_id):
    if session_id not in conversation_cache:
        conversation_cache[session_id] = []
    conversation_history = conversation_cache.get(session_id)
    return conversation_history


def put_session_id_msg(session_id, customer_id):
    session_id_msg[session_id] = customer_id


def get_session_id_msg(session_id):
    return session_id_msg.get(session_id)
